//
//  SearchTextController.m
//  NC-Trendy
//
//  Created by LeungChaos on 16/2/26.
//  Copyright © 2016年 liang. All rights reserved.
//

#import "SearchTextController.h"
#import "SearchItemViewController.h"
#import "DeleteBtnCell.h"
@interface SearchTextController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *cSearchBar;

@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
///显示所有item的tableView
@property (nonatomic, weak) UITableView *tableView;
///保存所有item的数组
@property (nonatomic, strong) NSMutableArray<NSString *> *searchStrArray;
///保存所有item中最小的item_id
@property (nonatomic, assign) NSInteger min_id;
///保存当前搜索字符
@property (nonatomic, copy) NSString *currentStr;
//键盘返回键控制器
@property (nonatomic, strong) IQKeyboardReturnKeyHandler *keyHandler;

@end

@implementation SearchTextController

- (IBAction)searchBtn:(UIButton *)sender {
    //判断是否有输入文字
    if (![self.searchTextField.text isEqualToString:@""]) {
        /*
         保存一条历史纪录
         */
        [SearchStrDBTools insertSearchStr:self.searchTextField.text errorInfo:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        //下一个页面显示数据
        SearchItemViewController *searchView = [[SearchItemViewController alloc]init];
        searchView.searchDict = [NSDictionary dictionaryWithObject:self.searchTextField.text forKey:@"searchStr"];
        [self.navigationController pushViewController:searchView animated:YES];
    }
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        //取搜索栏y最大值
        CGFloat tableY = CGRectGetMaxY(self.cSearchBar.frame);
        //创建tableView
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, tableY, kScreenW, kScreenH - kTabBarH -tableY ) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        _tableView = tableView;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

//懒加载search文字数组
- (NSMutableArray *)searchStrArray {
    if (_searchStrArray == nil) {
        _searchStrArray = [NSMutableArray arrayWithObject:@"没有搜索记录"];
        
    }
    return _searchStrArray;
}

//设置键盘属性
- (void)keyboardHandler {
    self.keyHandler = [[IQKeyboardReturnKeyHandler alloc]initWithViewController:self];
    self.keyHandler.lastTextFieldReturnKeyType = UIReturnKeyDone;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self keyboardHandler];
    NSMutableArray *strArray = [SearchStrDBTools searchStrArrayWithErrorInfo:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    if (strArray) {
        [self.searchStrArray removeAllObjects];
        self.searchStrArray = strArray;
        [self.searchStrArray addObject:@"删除历史记录"];
        [self.tableView reloadData];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.searchTextField.text = @"";
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchStrArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *iden = @"norCell";
    if (indexPath.row == self.searchStrArray.count - 1 && self.searchStrArray.count > 1) {
        NSLog(@"index %ld %ld",(long)indexPath.row, (long)self.searchStrArray.count);
        return [DeleteBtnCell deleteCellWithTableView:tableView];
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        cell.textLabel.text = self.searchStrArray[indexPath.row];
        return cell;
    }
}

#pragma mark - UITableViewDelegate
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.searchStrArray.count - 1) {
        [SearchStrDBTools deleteAllSearchStrWithErrorInfo:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        self.searchStrArray = nil;
        [self.tableView reloadData];
    } else {
        self.searchTextField.text = self.searchStrArray[indexPath.row];
    }
}


@end


